<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn1">11111111</button>
    <script>
      const bucket = new WeakMap();
      const data = { text: "hello world" };
      let activeEffect;
      function effect(fn) {
        activeEffect = fn;
        fn();
      }
      const obj = new Proxy(data, {
        get(target, key) {
          console.log("get", key);
          if (!activeEffect) return target[key];
          let depsMap = bucket.get(target);
          console.log("depsMap", depsMap)
          if (!depsMap) {
            bucket.set(target, (depsMap = new Map()));
          }
          let deps = depsMap.get(key);
          console.log("deps", deps)
          if (!deps) {
            depsMap.set(key, (deps = new Set()));
          }
          deps.add(activeEffect);
          return target[key];
        },
        set(target, key, newVal) {
          console.log("set", key);
          target[key] = newVal;
          const depsMap = bucket.get(target);
          if(!depsMap) return;
          const effects = depsMap.get(key);
          effects && effects.forEach((fn) => fn());
         
        },
      });
      effect(() => {
        console.log("effect111")
        document.body.innerText = obj.text;
      });

      effect(() => {
        console.log("effect222")
        document.body.innerText = obj.text;
      });

      setTimeout(() => {
        obj.text = 6666;
      }, 1000);
    </script>
  </body>
</html>
